package com.zenithmind.version.pojo.domain;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 提交统计信息实体类 - 遵循单一职责原则，专门处理提交统计数据
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@EqualsAndHashCode(callSuper = true)
@TableName("version_commit_statistics")
@Schema(description = "提交统计信息实体")
public class CommitStatistics extends BaseEntity {

    /**
     * 提交ID
     */
    @Schema(description = "提交ID")
    private String commitId;

    /**
     * 变更文件数量
     */
    @Schema(description = "变更文件数量")
    private Integer changedFiles;

    /**
     * 新增行数
     */
    @Schema(description = "新增行数")
    private Integer addedLines;

    /**
     * 删除行数
     */
    @Schema(description = "删除行数")
    private Integer deletedLines;

    /**
     * 提交大小（字节）
     */
    @Schema(description = "提交大小（字节）")
    private Long size;

    /**
     * 获取总变更行数
     * 
     * @return 总变更行数
     */
    public int getTotalChangedLines() {
        int added = addedLines != null ? addedLines : 0;
        int deleted = deletedLines != null ? deletedLines : 0;
        return added + deleted;
    }
    
    /**
     * 获取净变更行数（新增 - 删除）
     * 
     * @return 净变更行数
     */
    public int getNetChangedLines() {
        int added = addedLines != null ? addedLines : 0;
        int deleted = deletedLines != null ? deletedLines : 0;
        return added - deleted;
    }
    
    /**
     * 检查是否为大型提交
     * 
     * @return 是否为大型提交（变更超过100行或文件超过10个）
     */
    public boolean isLargeCommit() {
        return getTotalChangedLines() > 100 || 
               (changedFiles != null && changedFiles > 10);
    }
    
    /**
     * 获取格式化的提交大小
     * 
     * @return 格式化的大小字符串
     */
    public String getFormattedSize() {
        if (size == null || size == 0) {
            return "0 B";
        }
        
        String[] units = {"B", "KB", "MB", "GB"};
        int unitIndex = 0;
        double sizeValue = size.doubleValue();
        
        while (sizeValue >= 1024 && unitIndex < units.length - 1) {
            sizeValue /= 1024;
            unitIndex++;
        }
        
        return String.format("%.1f %s", sizeValue, units[unitIndex]);
    }
}
